/**
 *    Copyright 2009-2015 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.logging.jdbc;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.ibatis.logging.Log;
import org.apache.ibatis.reflection.ExceptionUtil;
import org.apache.log4j.Logger;
import org.lora.log4j.Log4jUtil;

/**
 * PreparedStatement proxy to add logging
 * 
 * @author Clinton Begin
 * @author Eduardo Macarron
 * 
 */
public final class PreparedStatementLogger extends BaseJdbcLogger implements InvocationHandler {

	private PreparedStatement statement;
	
	

	private PreparedStatementLogger(PreparedStatement stmt, Log statementLog, int queryStack) {
		super(statementLog, queryStack);
		this.statement = stmt;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
		try {
			if (Object.class.equals(method.getDeclaringClass())) {
				return method.invoke(this, params);
			}
			if (EXECUTE_METHODS.contains(method.getName())) {
				
		        if (mybatisLog.isDebugEnabled()) {
		        	mybatisLog.debug("["+proxy.hashCode()+"]Parameters: " + getParameterValueString());
		          }
				clearColumnInfo();
				if ("executeQuery".equals(method.getName())) {
					long time0 = System.currentTimeMillis();
					ResultSet rs = (ResultSet) method.invoke(statement, params);
					long time1 = System.currentTimeMillis();
					if(mybatisLog.isInfoEnabled()){
						mybatisLog.info(prefix(false) + "["+proxy.hashCode()+"] SQL Query Cost[" + (time1 - time0) + "]ms");
					}
					
					return rs == null ? null : ResultSetLogger.newInstance(rs, statementLog, queryStack);
				} else {
					long time0 = System.currentTimeMillis();
					Object result = method.invoke(statement, params);
					long time1 = System.currentTimeMillis();
					if(mybatisLog.isInfoEnabled()){
						mybatisLog.info(prefix(false) + "["+proxy.hashCode()+"] SQL Excute Cost[" + (time1 - time0) + "]ms");
					}
					
					return result;
				}
			} else if (SET_METHODS.contains(method.getName())) {
				if ("setNull".equals(method.getName())) {
					setColumn(params[0], null);
				} else {
					setColumn(params[0], params[1]);
				}
				return method.invoke(statement, params);
			} else if ("getResultSet".equals(method.getName())) {
				ResultSet rs = (ResultSet) method.invoke(statement, params);
				return rs == null ? null : ResultSetLogger.newInstance(rs, statementLog, queryStack);
			} else if ("getUpdateCount".equals(method.getName())) {
				int updateCount = (Integer) method.invoke(statement, params);
				if (updateCount != -1) {
					if(mybatisLog.isInfoEnabled()){
						mybatisLog.info(prefix(false) + "   Updates: " + updateCount);
					}
				}
				return updateCount;
			} else {
				return method.invoke(statement, params);
			}
		} catch (Throwable t) {
			throw ExceptionUtil.unwrapThrowable(t);
		}
	}

	/*
	 * Creates a logging version of a PreparedStatement
	 * 
	 * @param stmt - the statement
	 * 
	 * @param sql - the sql statement
	 * 
	 * @return - the proxy
	 */
	public static PreparedStatement newInstance(PreparedStatement stmt, Log statementLog, int queryStack) {
		InvocationHandler handler = new PreparedStatementLogger(stmt, statementLog, queryStack);
		ClassLoader cl = PreparedStatement.class.getClassLoader();
		return (PreparedStatement) Proxy.newProxyInstance(cl, new Class[] { PreparedStatement.class, CallableStatement.class }, handler);
	}

	/*
	 * Return the wrapped prepared statement
	 * 
	 * @return the PreparedStatement
	 */
	public PreparedStatement getPreparedStatement() {
		return statement;
	}
	
	public String showSql(String originSql){
		return getShowSql(originSql);
	}
	

}
